<?php

namespace app\controller;

use app\BaseController;
use app\model\BaseConfig;
use thans\jwt\facade\JWTAuth;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V6<br/><span style="font-size:30px">13载初心不改 - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,'.$name;
    }

    public function time()
    {
        return date('Y-m-d H:i:s');
    }

    public function test()
    {
        return json(['status' => 'o1k', 'message' => 'success']);
    }

    public function phpinfo()
    {
        echo phpinfo();
    }

    public function uuid_create()
    {
        echo  uuid_create();
    }

    public function redisTest($value = 123)
    {
        cache('name1', $value, 3600);
        echo cache('name1');
    }

    public function session()
    {
        session('test', 456);

        return session('test');
    }

    public function login()
    {
        $token = JWTAuth::builder(['uid' => 1]); //参数为用户认证的信息，请自行添加
        return json(['token' => $token]);
    }

    public function check(\think\Request $request)
    {
        $tokenInfo = $request->tokenInfo;
        $uid = $tokenInfo['uid'];

        return json(['uidInToken' => $uid]);
    }

    public function refreshToken()
    {
        try {
            $token = JWTAuth::refresh();

            return json(['token' => $token]);
        } catch (\Exception $e) {
            return json(['msg' => 'token不合法']);
        }
    }

    public function logout(\think\Request $request, BaseConfig $configModel)
    {
        JWTAuth::invalidate($request->token);

        return json(['msg' => '注销成功']);
    }
}
